<script setup>
import { useUserStore } from '@/stores/userStore.js'
import Section from '@/components/Section.vue'
import { reactive, ref } from 'vue'
import { delUser, updateUserInfo } from '@/api/user.js'
import VideoDrawer from '@/components/VideoDrawer.vue'
import { addVideoApi } from '@/api/video.js'

const userStore = useUserStore()
const dialog = ref(false)
const handleEdit = () => {
  dialog.value = true
}
// 上传图片
const uploadAvatar = (file) => {
  console.log(file)
  if (file.file.size / 1024 / 1024 > 2) {
    return ElMessage.error('上传图片大小不能超过 2MB!')
  }
  //TODO 发送请求上传图片（这里后端接口未完成，所以无法实现修改头像）
  const tempUrl = URL.createObjectURL(file.file)
  console.log(tempUrl)
  userStore.user.avatar = tempUrl
  newUserInfo.avatar = tempUrl
}
const newUserInfo = reactive({
  avatar: userStore.user?.avatar,
  name: userStore.user?.name,
  gender: userStore.user?.gender,
  age: userStore.user?.age,
  phone: userStore.user?.phone,
  address: userStore.user?.address
})
//确认修改
const onConfirmEdit = async () => {
  //发送请求
  const user = {
    id: userStore.user.id,
    headshot: newUserInfo.avatar,
    sex: newUserInfo.gender,
    name: newUserInfo.name,
    age: newUserInfo.age,
    phone: newUserInfo.phone,
    address: newUserInfo.address
  }
  const res = await updateUserInfo(userStore.user.uid, user)
  //修改本地
  userStore.setUser(newUserInfo)
  dialog.value = false
  ElMessage.success('修改成功')
}
const onCancelEdit = () => {
  dialog.value = false
  ElMessage.info('已取消修改')
}
const logout = () => {
  ElMessageBox.confirm(
    '是否确认退出',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '手滑了',
      type: 'warning'
    }
  )
    .then(() => {
      userStore.logout()
      ElMessage.success('已退出登录')
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消'
      })
    })
  // 刷新页面
  // window.location.reload()
}

/**
 * 注销
 */
const handleLogout = async () => {
  ElMessageBox.confirm(
    '是否确认注销当前登录账号',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '手滑了',
      type: 'error'
    }
  )
    .then(async () => {
      await delUser(userStore.user.id)
      userStore.logout()
      ElMessage.success('注销成功')
      dialog.value = false
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消'
      })
    })
}

const videoDrawer = ref()
/**
 *  添加视频
 */
const onAddVideo = () => {
  videoDrawer.value.drawer = true
}
const handleUpVideo = async (video) => {
  // 发送请求
  await addVideoApi(video)
  videoDrawer.value.drawer = false
  ElMessage.success('添加成功')
}
</script>

<template>
  <div v-if="userStore.user" class="profile">
    <div class="profile-content">
      <el-button color="#39c5bb" icon="Plus" plain round style="position: absolute;right: 10px;bottom: 10px"
                 @click="onAddVideo">添加视频
      </el-button>
      <div class="profile-content-left" style="position: relative">
        <el-avatar :src="userStore.user.avatar" style="width: 150px;height: 150px;position:relative;overflow: hidden">
        </el-avatar>
        <div style="font-size: 20px;font-weight: bold;display: flex;align-items: center">{{ userStore.user.name }}
          <el-tag size="small" style="color: #39c5bb;font-size: 12px;margin-left: 10px">{{ userStore.user.role === 0 ? '管理员' : '' }}
          </el-tag>
        </div>
        <el-button color="#39c5bb" icon="Setting" plain style="font-size: 15px" @click="handleEdit">个人设置</el-button>
        <el-button icon="Right" plain size="small"
                   style="font-size: 12px; margin: 0 " type="info"
                   @click="logout">退出登录
        </el-button>
      </div>
      <div class="profile-content-right">
        <Section :text="userStore.user?.name" title="姓名"></Section>
        <Section :text="userStore.user?.gender" title="性别"></Section>
        <Section :text="userStore.user?.age.toString()" title="年龄"></Section>
        <Section :text="userStore.user?.phone.toString()" title="电话"></Section>
        <Section :text="userStore.user?.address" title="地址"></Section>
      </div>
    </div>
  </div>
  <div v-else>
    <el-empty description="请先登录哟~"></el-empty>
  </div>
  <el-dialog v-model="dialog" title="编辑资料">
    <div class="edit">
      <div class="edit-item">
        <div class="name">头像</div>
        <el-upload
          :http-request="uploadAvatar"
          :show-file-list="false"
          class="avatar-uploader"
        >
          <img v-if="userStore.user.avatar" :src="userStore.user.avatar" alt="头像" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon">
            <Plus />
          </el-icon>
        </el-upload>
      </div>
      <el-form
        :model="newUserInfo"
        label-position="left"
        label-width="auto"
        style="width: 300px"
      >
        <el-form-item label="姓名">
          <el-input v-model="newUserInfo.name" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="newUserInfo.gender">
            <el-radio value="男">男</el-radio>
            <el-radio value="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="newUserInfo.age" />
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="newUserInfo.phone" />
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="newUserInfo.address" />
        </el-form-item>
      </el-form>
    </div>
    <div style="display: flex;justify-content: flex-end;margin-top: 50px">
      <el-button color="#39c5bb" plain round @click="onConfirmEdit">确认</el-button>
      <el-button plain round type="info" @click="onCancelEdit">取消</el-button>
    </div>
    <el-button plain size="small" style="position: absolute;left: 5%;bottom: 5%" type="danger" @click="handleLogout">
      注销账号
    </el-button>

  </el-dialog>
  <VideoDrawer ref="videoDrawer" :title="'添加视频'" @onSubmit="handleUpVideo"></VideoDrawer>
</template>

<style lang="scss" scoped>
.profile {
  width: 100%;
  display: flex;
  justify-content: center;
  padding-top: 50px;

  &-content {
    width: 60%;
    background-color: #fff;
    display: flex;
    justify-content: space-between;
    gap: 20px;
    border-radius: 10px;
    box-shadow: 5px 5px 10px #ccc;
    position: relative;

    &-left {
      flex: 3;
      border-right: 1px solid #ccc;
      display: flex;
      justify-content: center;
      flex-direction: column;
      align-items: center;
      gap: 20px;
    }

    &-right {
      flex: 5;
    }
  }
}

.edit {
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;

  &-item {
    display: flex;
    font-size: 14px;
    justify-content: space-between;
    align-items: center;
    gap: 100px;
    width: 300px;
  }

  .avatar-uploader .avatar {
    width: 80px;
    height: 80px;
    display: block;
  }
}
</style>